package team.hyznrj.studentsys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.hyznrj.studentsys.dao.mapper.AccountInfoMapper;
import team.hyznrj.studentsys.dao.repository.account.AccountInfoRepository;
import team.hyznrj.studentsys.dao.repository.account.RoleInfoRepository;
import team.hyznrj.studentsys.dao.repository.account.RolePowerTableRepository;
import team.hyznrj.studentsys.dto.PowerDto;
import team.hyznrj.studentsys.dto.RoleDto;
import team.hyznrj.studentsys.entity.account.AccountInfo;
import team.hyznrj.studentsys.entity.account.RoleInfo;
import team.hyznrj.studentsys.entity.account.RolePowerTable;
import team.hyznrj.studentsys.entity.account.key.RolePowerTableKey;
import team.hyznrj.studentsys.enums.AccountEnum;
import team.hyznrj.studentsys.exception.AccountException;
import team.hyznrj.studentsys.service.RoleService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author ReMidDream
 * @date 2018-05-07 14:39
 **/
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private AccountInfoMapper accountInfoMapper;
    @Autowired
    private RoleInfoRepository roleInfoRepository;
    @Autowired
    private RolePowerTableRepository rolePowerTableRepository;
    @Autowired
    private AccountInfoRepository accountInfoRepository;

    @Override
    public List<String> findRoleId(List<String> roleLovellId) {
        return accountInfoMapper.findRoleId(roleLovellId);
    }

    @Override
    public RoleDto findRoleDtoByRoleId(String roleId) {
        return accountInfoMapper.findRoleDtoByRoleId(roleId);
    }

    @Override
    public List<RoleDto> findRoleDtoByRoleIds(Set<String> roleIds) {
        return accountInfoMapper.findRoleDtoByRoleIds(roleIds);
    }

    @Override
    public List<RoleDto> findAllRoleDto(List<String> roleId) {
        return accountInfoMapper.findAllRoleDto(roleId);
    }

    @Override
    public void deleteRelationshipBetweenRoleAndPower(RolePowerTableKey rolePowerTableKey) {
        rolePowerTableRepository.delete(rolePowerTableKey);
    }

    @Override
    public void insertRelationshipBetweenRoleAndPower(RolePowerTableKey rolePowerTableKey) {

        //todo 合法性认证

        RolePowerTable rolePowerTable = new RolePowerTable();
        rolePowerTable.setId(rolePowerTableKey);
        rolePowerTableRepository.save(rolePowerTable);
    }

    @Override
    @Transactional
    public void deleteRole(String roleId) {
        List<AccountInfo> accountInfoList = accountInfoRepository.findByRoleId(roleId);

        if (accountInfoList.size() >0) {
            log.error(" @删除角色 : 此功能已被账号引用 roleId={}",roleId);
            throw new AccountException(AccountEnum.TABLE_ERROR);
        }

        roleInfoRepository.delete(roleId);
        rolePowerTableRepository.deleteByIdRptRoleId(roleId);
    }

    @Override
    @Transactional
    public void insertUpdateRole(RoleDto roleDto) {
        RoleInfo roleInfo = new RoleInfo();
        List<RolePowerTable> rolePowerTables =new ArrayList<>();

        /*
            roleInfo 表插入/更新
         */
        BeanUtils.copyProperties(roleDto,roleInfo);
        roleInfoRepository.save(roleInfo);

        /*
            中间表插入
         */
        if (roleDto.getPowerDtoList() != null) {

            List<RolePowerTable> rolePowerTableList = rolePowerTableRepository.findByIdRptRoleId(roleDto.getRoleId());

            if (rolePowerTableList.size() != 0) {
                rolePowerTableRepository.deleteByIdRptRoleId(roleDto.getRoleId());
            }

            for (PowerDto powerDto : roleDto.getPowerDtoList()) {
                RolePowerTable rolePowerTable = new RolePowerTable();
                RolePowerTableKey rolePowerTableKey = new RolePowerTableKey();

                rolePowerTableKey.setRptRoleId(roleDto.getRoleId());
                rolePowerTableKey.setRptPowerId(powerDto.getPowerId());

                rolePowerTable.setId(rolePowerTableKey);
                rolePowerTables.add(rolePowerTable);
            }
            rolePowerTableRepository.save(rolePowerTables);
        }
    }



}
